package main

import (
	"log"
	"net/rpc"
	"net"
	"net/rpc/jsonrpc"
	"gitee.com/Fanghr/FuckRPC_DataStructure"
)

const port string = ":8989"

func checkError(err error) {
	if err != nil {
		log.Panicln("Panic error:", err.Error())
	}
}

func main() {
	arith := new(RPCDataStructure.Arith)
	rpc.Register(arith)

	tcpAddr, err := net.ResolveTCPAddr("tcp", port)
	checkError(err)
	Listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	log.Println("RPCServer start at IP:", tcpAddr.IP, ",", "Port:", tcpAddr.Port)

	for {
		conn, err := Listener.Accept()
		if err != nil {
			log.Println("Accept connection error:", err.Error())
			continue
		}
		log.Println("Connection Accept:", conn.RemoteAddr())
		jsonrpc.ServeConn(conn)
	}
}
